package com.neo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.neo.constant.MessageConstant;
import com.neo.entity.PageResult;
import com.neo.entity.QueryPageBean;
import com.neo.entity.Result;
import com.neo.pojo.CheckItem;
import com.neo.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查项
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     * 检查项新增
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return Result.ok(MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    /**
     *  检查项分页
     * @param pageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        try {
            return checkItemService.findPage(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL;
        }
    }


    /**
     * 检查项删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        try {
            checkItemService.delete(id);
            return Result.ok(MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e){
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 检查项查找
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("id")  Integer id){
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     *  检查项更新
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
            return Result.ok(MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 查询所有检查项
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> checkItemList = checkItemService.findAll();
            return Result.ok(MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
